package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author benben278
 * @create 2022-03-14 14:16
 */
@Mapper
public interface SetmealDao {
    //新增套餐数据
    @Insert("insert into t_setmeal (code,name,sex,age,helpCode,price,remark,attention,img)\n" +
            "values (#{code},#{name},#{sex},#{age},#{helpCode},#{price},#{remark},#{attention},#{img})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void add(Setmeal setmeal);
    //设置检查组和套餐的关联关系
    @Insert("insert into t_setmeal_checkgroup (setmeal_id,checkgroup_id) values (#{setmeal_id},#{checkgroup_id})")
    void setSetmealAndCheckGroup(Map map);
    //根据条件查询
    @Select("<script> select * from t_setmeal <if test=\"value != null and value.length > 0\"> where code = #{value} or name = #{value} or helpCode = #{value} </if> </script>")
    Page<Setmeal> findByCondition(String queryString);
    //查询所有套餐
    @Select("select * from t_setmeal")
    List<Setmeal> findAll();
    //根据套餐ID查询套餐详细信息（包括套餐基本信息、套餐包含的检查组、检查组包含的检查项），这一块没搞懂
    @Select("<resultMap type=\"com.itheima.pojo.Setmeal\" id=\"baseResultMap\">\n" +
            "        <id column=\"id\" property=\"id\"/>\n" +
            "        <result column=\"name\" property=\"name\"/>\n" +
            "        <result column=\"code\" property=\"code\"/>\n" +
            "        <result column=\"helpCode\" property=\"helpCode\"/>\n" +
            "        <result column=\"sex\" property=\"sex\"/>\n" +
            "        <result column=\"age\" property=\"age\"/>\n" +
            "        <result column=\"price\" property=\"price\"/>\n" +
            "        <result column=\"remark\" property=\"remark\"/>\n" +
            "        <result column=\"attention\" property=\"attention\"/>\n" +
            "        <result column=\"img\" property=\"img\"/>\n" +
            "    </resultMap>\n" +
            "    <resultMap type=\"com.itheima.pojo.Setmeal\" id=\"findByIdResultMap\" extends=\"baseResultMap\"> \n" +
            "        <collection property=\"checkGroups\"\n" +
            "                    javaType=\"ArrayList\"\n" +
            "                    ofType=\"com.itheima.pojo.CheckGroup\"\n" +
            "                    column=\"id\"\n" +
            "                    select=\"com.itheima.dao.CheckGroupDao.findCheckGroupById\"><!--关联CheckGroupDao.xml中findCheckGroupById-->\n" +
            "        </collection>\n" +
            "    </resultMap>\n" +
            "    <select id=\"findById\" parameterType=\"int\" resultMap=\"findByIdResultMap\">\n" +
            "        select * from t_setmeal  where id=#{id}\n" +
            "    </select>")
    Setmeal findById(Integer id);
    //查询套餐预约占比
    @Select("select s.name,count(o.id) as value from t_order o ,t_setmeal s where o.setmeal_id = s.id group by s.name")
    List<Map<String, Object>> findSetmealCount();
}
